package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     *
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R add(@RequestBody Orders orders, HttpSession session) {
        User user = (User) session.getAttribute("User");
        int add = orderService.add(orders, user);
        if (add > 0) return R.success("添加成功");
        else return R.error("添加失败");
    }

    /**
     * 后端查询订单明细
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R list(int page, int pageSize, Long number, String beginTime, String endTime) {
        try {
            //  User user = (User)session.getAttribute("User");

            System.out.println(beginTime);
            System.out.println(endTime);
            IPage<OrdersDto> page1 = orderService.findPage(page, pageSize, number, beginTime, endTime);
            return R.success(page1);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("订单查询失败");
        }

    }

    /**
     * 前端查询订单明细
     *
     * @param pageClass
     * @param session
     * @return
     */
    @GetMapping("/userPage")
    public R userPage(PageParam pageClass, HttpSession session) {
        try {
            User user = (User) session.getAttribute("User");
            IPage<OrdersDto> userPage = orderService.findUserPage(pageClass, user);
            return R.success(userPage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("订单详情失败");
        }
    }
}
